<template>
  <div>
    <el-dialog :title="formData.id?'编辑客户信息':'新增客户'" :visible="dialogVisible" width="80%" @close="closeFn">
      <!-- 编辑或者新增表单 -->
      <el-form ref="form" :rules="rules" :model="formData" label-width="80px">
        <el-form-item label="经纪" prop="agent">
          <el-select v-model="formData.agent">
            <el-option
              v-for="item in agentList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="微信" prop="wechat">
          <el-input v-model="formData.wechat" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" />
        </el-form-item>
        <el-form-item label="封面" prop="image">
          <!-- <el-input v-model="formData.image"></el-input> -->
          <!-- 上传图片 -->
          <el-upload class="avatar-uploader" action="" :on-change="imgSuccess">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addCustomer, editCustomerInfo } from '@/api/customer'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    },
    agentList: { // 经纪人列表
      type: Array,
      default: []
    }
  },
  data() {
    return {
      formData: {
        agent: null, // 属于哪个经纪的 id 值
        name: '', // 名称
        email: '', // 电子邮箱，比如 example@qq.com
        phone: '', // 电话
        wechat: '', // 微信
        desc: '', // 描述
        image: '' // 图片}
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        agent: [
          { required: true, message: '请选择经纪人', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '输入正确的手机格式', trigger: 'blur' }
        ],
        wechat: [
          { required: true, message: '请输入微信', trigger: 'blur' }
        ],
        image: [
          { required: true, message: '请输入图片', trigger: 'blur' }
        ]
      },
      imageUrl: ''
    }
  },
  methods: {
    closeFn() { // 关闭弹窗
      this.$emit('update:dialogVisible', false)
      this.formData = {
        agent: null, // 属于哪个经纪的 id 值
        name: '', // 名称
        email: '', // 电子邮箱，比如 example@qq.com
        phone: '', // 电话
        wechat: '', // 微信
        desc: '', // 描述
        image: '' // 图片}
      }
      this.$refs.form.resetFields()
      this.imageUrl = ''
    },
    submit() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          console.log(this.formData.id)
          if (this.formData.id) {
            const res = await editCustomerInfo(this.formData)
            console.log(res)
          } else {
            const res = await addCustomer(this.formData)
            console.log(res)
          }

          this.closeFn()
          this.$emit('updataList')
          this.$emit('updataNum')
        }
      })
    },
    imgSuccess() {
      this.imageUrl = 'https://tupian.qqw21.com/article/UploadPic/2020-1/20201122246510666.jpg'
      this.formData.image = 'https://tupian.qqw21.com/article/UploadPic/2020-1/20201122246510666.jpg'
    }
  }
}
</script>
<style lang="scss" scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dashed #ccc;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
